/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FileJFrame.java
 *
 * Created on 23/08/2010, 15:51:08
 */

package br.com.barrientosjunior.blackup.swing;

import br.com.barrientosjunior.blackup.MainManager;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Junior
 */
public class FileJFrame extends javax.swing.JFrame {

    private MainManager manager = null;

    private java.io.File inputFile = null;
    private java.io.File outputFile = null;

    /** Creates new form FileJFrame */
    public FileJFrame(MainManager manager) {
        this.manager = manager;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        origemJTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        destinoJTextField = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jCheckBoxEncrypt = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldPass = new javax.swing.JTextField();
        jCheckBoxRecordPass = new javax.swing.JCheckBox();
        jButtonCancel = new javax.swing.JButton();
        jButtonSave = new javax.swing.JButton();

        jLabel1.setText("Arquivo ou pasta de origem:");

        origemJTextField.setEditable(false);

        jButton1.setText("Escolher...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Pasta de destino:");

        destinoJTextField.setEditable(false);

        jButton2.setText("Escolher...");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jCheckBoxEncrypt.setText("Criptografar backup");
        jCheckBoxEncrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxEncryptActionPerformed(evt);
            }
        });

        jLabel3.setText("Senha:");
        jLabel3.setEnabled(false);

        jTextFieldPass.setEnabled(false);

        jCheckBoxRecordPass.setText("Salvar senha");
        jCheckBoxRecordPass.setEnabled(false);

        jButtonCancel.setText("Cancelar");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jButtonSave.setText("Salvar");
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBoxRecordPass)
                    .addComponent(jCheckBoxEncrypt)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(destinoJTextField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(origemJTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(jButton1)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldPass, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonSave)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(origemJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(destinoJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBoxEncrypt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxRecordPass)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancel)
                    .addComponent(jButtonSave))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        manager.closeFileFrame();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Selecione o arquivo ou pasta para backup");
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fileChooser.setMultiSelectionEnabled(false);

        int retVal = fileChooser.showDialog(this, "Selecionar");
        if (retVal == 0){
                inputFile = fileChooser.getSelectedFile();
                this.origemJTextField.setText(inputFile.getPath());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Selecione a pasta destino do backup");
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setMultiSelectionEnabled(false);

        int retVal = fileChooser.showDialog(this, "Selecionar");
        if (retVal == 0){
            outputFile = fileChooser.getSelectedFile();
            this.destinoJTextField.setText(outputFile.getPath());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        br.com.barrientosjunior.blackup.model.File file = new br.com.barrientosjunior.blackup.model.File();
        file.isEncrypted(this.getjCheckBoxEncrypt().isSelected());
        //file.isEncrypted(this.getjCheckBoxEncrypt().isSelected());
        // critica de manter senha
        if(file.isEncrypted()){
            if(this.getjTextFieldPass().getText().trim().equals(""))
                JOptionPane.showMessageDialog(this, "Insira uma senha.");
            else{
                file.setPass(this.jTextFieldPass.getText());
                file.isEncrypted(true);
            }
        }else{
            
        }
        file.setKeepOldCopies(false);
        file.setPathOrigin(inputFile.getPath());
        file.setLastModified(inputFile.lastModified());
        file.setPathDestiny(outputFile.getPath() + "\\" + inputFile.getName());
        manager.addFile(file);

        this.setVisible(false);
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jCheckBoxEncryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxEncryptActionPerformed
        jTextFieldPass.setEnabled((jTextFieldPass.isEnabled())? false : true);
        jLabel3.setEnabled((jLabel3.isEnabled())? false : true);
        jCheckBoxRecordPass.setEnabled((jCheckBoxRecordPass.isEnabled())? false : true);
    }//GEN-LAST:event_jCheckBoxEncryptActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField destinoJTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JCheckBox jCheckBoxEncrypt;
    private javax.swing.JCheckBox jCheckBoxRecordPass;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextFieldPass;
    private javax.swing.JTextField origemJTextField;
    // End of variables declaration//GEN-END:variables

    public JTextField getDestinoJTextField() {
        return destinoJTextField;
    }

    public JCheckBox getjCheckBoxEncrypt() {
        return jCheckBoxEncrypt;
    }

    public JCheckBox getjCheckBoxRecordPass() {
        return jCheckBoxRecordPass;
    }

    public JTextField getjTextFieldPass() {
        return jTextFieldPass;
    }

    public JTextField getOrigemJTextField() {
        return origemJTextField;
    }

}
